
package ip.youtube;

import java.sql.SQLException;
import java.util.List;

import javax.persistence.EntityManager;
import javax.persistence.EntityManagerFactory;
import javax.persistence.EntityTransaction;
import javax.persistence.Persistence;
import javax.persistence.Query;

import org.hibernate.HibernateException;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.cfg.Configuration;

public class UserDAO implements DAOInterface{
	
	private static final SessionFactory sessionFactory;
	static {
		try {
			sessionFactory = new Configuration().configure()
					.buildSessionFactory();
		} catch (Throwable ex) {
			System.out.println("Initial SessionFactory creation failed." + ex);
			throw new ExceptionInInitializerError(ex);
		}
	}

	public static SessionFactory getSessionFactory() {
		return sessionFactory;
	}

	public List<Video> findAllVideos(int user_id) {
		EntityManagerFactory factory =
				 Persistence.createEntityManagerFactory("JPA" );		
			EntityManager em = factory.createEntityManager();
			EntityTransaction ta = em.getTransaction();
			ta.begin();
			Query query = em.createQuery("SELECT v FROM videos v, users u where v.user_id = user_id"  );
			List<Video> resultList = query.getResultList();		
			ta.commit();
			em.close();
			
			return resultList;
	}
	
	public User getUserByName(String name){
		
		EntityManagerFactory factory =
				 Persistence.createEntityManagerFactory("JPA" );		
			EntityManager em = factory.createEntityManager();
			EntityTransaction ta = em.getTransaction();
			ta.begin();
			Query query = em.createQuery("SELECT User_Id FROM users u, users u where u.Username = name"  );
			String Id = (String) query.getSingleResult();	
			ta.commit();
			em.close();
			
			Session session = sessionFactory.openSession(); 
			 
			User user = (User) session.load(User.class, new Integer(Id)); 
			session.close(); 
		
		return user;
		
	}
	
	public int registerNewUser(String name, String password){
		
		EntityManagerFactory factory =
				 Persistence.createEntityManagerFactory("JPA" );		
			EntityManager em = factory.createEntityManager();
			EntityTransaction ta = em.getTransaction();
			ta.begin();
			Query query = em.createQuery("SELECT User_Id FROM users u, users u where u.Username = name"  );
			List<User> resultList = query.getResultList();	
			ta.commit();
			em.close();
			
			if(!resultList.isEmpty()){
				return 0;
			}
			
			else{
		
				Session session = sessionFactory.openSession(); 
				
				User user = new User();
				user.setUsername(name);
				user.setPassword(password);
				
				session.save(user); 
				session.flush(); 
				try {
					session.connection().commit();
				} catch (HibernateException e) {
					System.out.println("Hibernate session connection failed " + e);
					e.printStackTrace();
				} catch (SQLException e) {
					System.out.println("SQL commit failed " + e);
					e.printStackTrace();
				} 
				session.close();	
				
				return user.getUser_Id();
			}		
	}
	
}

